<?php
/**
 * 系统权限检验接口
 * User: Administrator
 * Date: 13-6-10
 * Time: 下午1:54
 */
abstract class CommonAction extends Action
{
    protected  $_mode;

    public function _initialize(){
        //设置tab的随机值，用于打开同一页面不造成冲突
        $this->assign('tab_id', md5(uniqid(mt_rand(), true)));
    }

    //显示首页模板
    public function index(){
        $this->display();
    }

    /**
     * 获取单条记录
     * @param $itemid
     * @return mixed
     */
    public function item($itemid){
        $data = $this->_mode->find($itemid);
        die(json_encode($data));
    }

    /**
     * 删除记录
     * @param $itemid
     */
    public function drop(){
        if(!IS_AJAX) return;
        $itemid = $this->_request('itemid');
        if(empty($itemid)) ajax_return(L('item_lost'), 0);
        if($this->_mode->delete($itemid)){
            ajax_return(L('drop_success'));
        }else{
            ajax_return(L('drop_error'), 0);
        }
    }

    /**
     * 保存记录
     */
    public function save(){
        $data = $this->_mode->create();
        $primary = $this->_mode->getPk();
        if(isset($data[$primary]) && !empty($data[$primary])){
            $result = $this->_mode->save();
        }else{
            $result = $this->_mode->add();
        }
        if($result){
            ajax_return(L('save_success'));
        }else{
            ajax_return(L('save_error'), 0);
        }
    }

    /**
     * 列表
     */
    public function query(){
        $data = $this->_mode->fetch_datagrid();
        die(json_encode($data));
    }
}